<?php
	
	function helper($helper_name)
	{
		if(file_exists(PROJECT_DIR . "app/helpers/{$helper_name}_helper.php"))
		{
			include(PROJECT_DIR . "app/helpers/{$helper_name}_helper.php");
		}
	}
	
	function system_library($library_name)
	{
		if(file_exists(SYSTEM_DIR . "libraries/{$library_name}.library.php"))
		{
			include(SYSTEM_DIR . "libraries/{$library_name}.library.php");
		}
	}
	
	function library($library_name)
	{
		$file_name = uncamelize($library_name);
		if(file_exists(PROJECT_DIR . "library/libraries/$library_name/$file_name.php"))
		{
			include(PROJECT_DIR . "library/libraries/$library_name/$file_name.php");
		}else{
			trigger_error("$library_name Library was not found (" . PROJECT_DIR . "library/libraries/$library_name/$file_name.php)");
		}
	}
	
	/**
	 * Underscore
	 *
	 * Takes multiple words separated by spaces and underscores them
	 *
	 * @access	public
	 * @param	string
	 * @return	str
	 */
	function underscore($str)
	{
		return preg_replace('/[\s]+/', '_', strtolower(trim($str)));
	}
	
	/**
	 * Humanize
	 *
	 * Takes multiple words separated by underscores and changes them to spaces
	 *
	 * @access	public
	 * @param	string
	 * @return	str
	 */
	function humanize($str)
	{
		return ucwords(preg_replace('/[_]+/', ' ', strtolower(trim($str))));
	}
	
	/**
	 * Camelize
	 *
	 * Takes multiple words separated by spaces or underscores and camelizes them
	 *
	 * @access	public
	 * @param	string
	 * @return	str
	 */
	function camelize($str)
	{		
		$str = 'x'.strtolower(trim($str));
		$str = ucwords(preg_replace('/[\s_]+/', ' ', $str));
		return substr(str_replace(' ', '', $str), 1);
	}
	
	/**
	 * Uncamelize
	 *
	 * Takes camelized words makes them underscored
	 *
	 * @access	public
	 * @param	string
	 * @return	str
	 */
	function uncamelize($str)
	{
		$str = preg_replace("/[A-Z]/", "_\\0", $str);
		
		if($str[0] == "_")
		{
			$str = substr($str, 1, strlen($str));
		}
		
		$str = strtolower($str);
		return $str;
	}
	
	/**
	 * Lowercase
	 *
	 * Takes multiple words separated by spaces or underscores and camelizes them
	 *
	 * @access	public
	 * @param	string
	 * @return	str
	 */
	function lowercase($str)
	{
		return strtolower(trim($str));
	}
	
	/**
	 * Plural
	 *
	 * Takes a singular word and makes it plural
	 *
	 * @access	public
	 * @param	string
	 * @param	bool
	 * @return	str
	 */
	function plural($str, $force = FALSE)
	{
	    $str = strtolower(trim($str));
	    $end = substr($str, -1);

	    if ($end == 'y')
	    {
	        $str = substr($str, 0, strlen($str)-1).'ies';
	    }
	    elseif ($end == 's')
	    {
	        if ($force == TRUE)
	        {
	            $str .= 'es';
	        }
	    }
	    else
	    {
	        $str .= 's';
	    }

	    return $str;
	}
	
	function unplural($str, $force = FALSE)
	{
	    $str = strtolower(trim($str));
		
		if(preg_match("/ies$/", $str))
		{
			$str = preg_replace("/ies$/", "y", $str);
		}
		else if(preg_match("/ses$/", $str))
		{
			$str = preg_replace("/ses$/", "s", $str);
		}
		else if(preg_match("/[^s]s$/", $str))
		{
			$str = preg_replace("/([^s])(s)$/i", "$1", $str);
		}
		
	    return $str;
	}
	
	function truncate($string, $length = 10)
	{
		$orig = $string . "";
		$length = $length + 0;
		$string = $string . "";
		$string = substr( $string, 0, $length);
		
		if($orig != $string)
		{
			$string .= "<small>&hellip;</small>";
		}
		
		return $string;
	}
	
	function sanitize($var)
	{
		if(is_string($var))
		{
			$var = mysql_real_escape_string(stripslashes($var));
			return $var;
		}
		
		if(is_integer($var) || is_float($var))
		{
			$var = $var + 0;
			return $var;
		}
		
		return false;
	}
	
	function ordinal_suffix($int)
	{
		if($int == 1)
		{
			return "st";
		}
		
		else if($int == 2)
		{
			return "nd";
		}
		
		else if($int == 3)
		{
			return "rd";
		}
		
		else if($int > 4)
		{
			return "th";
		}
		
		else
		{
			return "";
		}
	}
	
	
	function forceSSL()
	{
		/*
		echo "<pre>\n";
		print_r($_SERVER);
		echo "</pre>";
		*/
		
		if(isset($_SERVER['HTTPS']))
		{
			if($_SERVER["HTTPS"] != "on")
			{
				$newurl = "https://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
				header("Location: $newurl");
				exit();
			}
		}
		else
		{
			$newurl = "https://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
			header("Location: $newurl");
			exit();
		}
		
	}
	
	function to_long_date($time = null)
	{
		if($time == null){ $time = time(); }
		
		$type = gettype($time);
		
		if($type == "string")
		{
			return date("g:ia F jS, Y", strtotime($time));
		}
		else if($type == "integer")
		{
			return date("g:ia F jS, Y", $time);
		}
		else{
			return false;
		}
	}
	
	function to_short_date($time = null, $plus_time = true)
	{
		if($time == null){ $time = date("U"); }
		
		if(is_string($time)){ $time = strtotime($time); }
		
		if($plus_time)
		{
			$date = date("n/j g:i a T", $time);
		}else{
			$date = date("n/j/Y", $time);
		}
		
		return $date;
	}
	
	function to_mysql_date_time($time = null)
	{
		if(is_string($time)){ $time = strtotime($time); }
		
		else if($time == null){ $time = date("U"); }
		
		return date("Y-m-d H:i:s", $time);
	}
	
	
?>